package com.empire.module.rainbow.controller.app.diy;

import com.empire.framework.common.util.object.BeanUtils;
import com.empire.module.rainbow.controller.app.diy.vo.AppRainbowDiyPageRespVO;
import com.empire.module.rainbow.controller.app.diy.vo.AppRainbowDiyTemplateRespVO;
import com.empire.module.rainbow.convert.diy.RainbowDiyTemplateConvert;
import com.empire.module.rainbow.dal.dataobject.diypage.RainbowDiyPageDO;
import com.empire.module.rainbow.enums.RainbowDiyPageEnum;
import com.empire.module.rainbow.service.diypage.RainbowDiyPageService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import java.util.*;

import com.empire.framework.common.pojo.CommonResult;
import static com.empire.framework.common.pojo.CommonResult.success;

import static com.empire.framework.common.util.collection.CollectionUtils.findFirst;

import com.empire.module.rainbow.dal.dataobject.diytemplate.RainbowDiyTemplateDO;
import com.empire.module.rainbow.service.diytemplate.RainbowDiyTemplateService;

@Tag(name = "用户 APP - 六彩-装修模板")
@RestController
@RequestMapping("/rainbow/diy-template")
@Validated
public class AppRainbowDiyTemplateController {

    @Resource
    private RainbowDiyTemplateService rainbowDiyTemplateService;
    @Resource
    private RainbowDiyPageService rainbowDiyPageService;

    // TODO @疯狂：要不要把 used 和 get 接口合并哈；不传递 id，直接拿默认；
    @GetMapping("/used")
    @Operation(summary = "使用中的装修模板")
    @PermitAll
    public CommonResult<AppRainbowDiyTemplateRespVO> getUsedDiyTemplate() {
        RainbowDiyTemplateDO diyTemplate = rainbowDiyTemplateService.getUsedDiyTemplate();
//        RainbowDiyTemplateDO diyTemplate = rainbowDiyTemplateService.getDiyTemplate(1L);
        return success(buildVo(diyTemplate));
    }

    @GetMapping("/get")
    @Operation(summary = "获得装修模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<AppRainbowDiyTemplateRespVO> getDiyTemplate(@RequestParam("id") Long id) {
        RainbowDiyTemplateDO diyTemplate = rainbowDiyTemplateService.getDiyTemplate(id);
        return success(buildVo(diyTemplate));
    }


    private AppRainbowDiyTemplateRespVO buildVo(RainbowDiyTemplateDO diyTemplate) {
        if (diyTemplate == null) {
            return null;
        }
        // 查询模板下的页面
        List<RainbowDiyPageDO> pages = rainbowDiyPageService.getDiyPageByTemplateId(diyTemplate.getId());
        String home = findFirst(pages, page -> RainbowDiyPageEnum.INDEX.getName().equals(page.getName()), RainbowDiyPageDO::getProperty);
        String user = findFirst(pages, page -> RainbowDiyPageEnum.MY.getName().equals(page.getName()), RainbowDiyPageDO::getProperty);
        // 拼接返回
        return RainbowDiyTemplateConvert.INSTANCE.convertPropertyVo2(diyTemplate, home, user);
    }
}
